/* ************************************************************************
*File Name       :Can.h

*File Functions :

*Explain          :
**************************************************************************/
#ifndef __stm8s_can_h
#define __stm8s_can_h

/* ************************************************************************
                                                       Includes File 
**************************************************************************/
//#include "stm8s_map.h"
#include "stm8s.h"

/* CAN Registers */
extern vu8 CAN_MCR		@0x5420;
extern vu8 CAN_MSR		@0x5421;
extern vu8 CAN_TSR		@0x5422;
extern vu8 CAN_TPR		@0x5423;
extern vu8 CAN_RFR		@0x5424;
extern vu8 CAN_IER		@0x5425;
extern vu8 CAN_DGR		@0x5426;
extern vu8 CAN_FPSR		@0x5427;
extern vu8 CAN_P0		@0x5428;
extern vu8 CAN_P1		@0x5429;
extern vu8 CAN_P2		@0x542a;
extern vu8 CAN_P3		@0x542b;
extern vu8 CAN_P4		@0x542c;
extern vu8 CAN_P5		@0x542d;
extern vu8 CAN_P6		@0x542e;
extern vu8 CAN_P7		@0x542f;
extern vu8 CAN_P8		@0x5430;
extern vu8 CAN_P9		@0x5431;
extern vu8 CAN_Pa		@0x5432;
extern vu8 CAN_Pb		@0x5433;
extern vu8 CAN_Pc		@0x5434;
extern vu8 CAN_Pd		@0x5435;
extern vu8 CAN_Pe		@0x5436;
extern vu8 CAN_Pf		@0x5437;
/*  Tx MailBox / Receive FIFO Registers */
extern vu8 CAN_MCSR		@0x5428;
extern vu8 CAN_MFMI		@0x5428;
extern vu8 CAN_MDLC		@0x5429;
extern vu8 CAN_MIDR1		@0x542a;
extern vu8 CAN_MIDR2		@0x542b;
extern vu8 CAN_MIDR3		@0x542c;
extern vu8 CAN_MIDR4		@0x542d;
extern vu8 CAN_MDAR[8]		@0x542e;
extern vu8 CAN_MTSLR		@0x5436;
extern vu8 CAN_MTSHR		@0x5437;
extern vu16 CAN_MIDR12		@0x542a;
extern vu16 CAN_MIDR34		@0x542c;
/*  Configuaration/Diagnosis Registers */
extern vu8 CAN_ESR  	@0x5428;
extern vu8 CAN_EIER 	@0x5429;
extern vu8 CAN_TECR  	@0x542a;
extern vu8 CAN_RECR  	@0x542b;
extern vu8 CAN_BTR1 	@0x542c;
extern vu8 CAN_BTR2 	@0x542d;
extern vu8 reserved1 	@0x542e;
extern vu8 reserved2 	@0x542f;
extern vu8 CAN_FMR1 	@0x5430;
extern vu8 CAN_FMR2 	@0x5431;
extern vu8 CAN_FCR1 	@0x5432;
extern vu8 CAN_FCR2 	@0x5433;
extern vu8 CAN_FCR3 	@0x5434;
extern vu8 reserved3 	@0x5435;
extern vu8 reserved4 	@0x5436;
extern vu8 reserved5 	@0x5437;
/*  Acceptance filter 0:1 Registers */
extern vu8 CAN_FxR0 	@0x5428;
extern vu8 CAN_FxR1 	@0x5429;
extern vu8 CAN_FxR2 	@0x542a;
extern vu8 CAN_FxR3 	@0x542b;
extern vu8 CAN_FxR4 	@0x542c;
extern vu8 CAN_FxR5 	@0x542d;
extern vu8 CAN_FxR6 	@0x542e;
extern vu8 CAN_FxR7 	@0x542f;
extern vu8 CAN_FyR0 	@0x5430;
extern vu8 CAN_FyR1 	@0x5431;
extern vu8 CAN_FyR2 	@0x5432;
extern vu8 CAN_FyR3 	@0x5433;
extern vu8 CAN_FyR4 	@0x5434;
extern vu8 CAN_FyR5 	@0x5435;
extern vu8 CAN_FyR6 	@0x5436;
extern vu8 CAN_FyR7 	@0x5437;
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

/* ************************************************************************
                                                       Exported macro 
**************************************************************************/



/* ************************************************************************
                                                       Exported variables 
**************************************************************************/

/* ************************************************************************
                                                       Exported functions 
**************************************************************************/

/* user transmmit structure */
typedef volatile struct
{
	unsigned int stdid;	// 11 bit
	unsigned long extid;	// 18 bit
	unsigned char dlc;
	unsigned char data[8];
//	unsigned char Flag;
} tCanMsgOject;

typedef enum 
{
	CFALSE=0,
	CTRUE=1
}CAN_Bool;//Misc.

/* user receive structure */
typedef volatile struct
{
	unsigned int stdid;
	unsigned long extid;
	unsigned int bit11id;
	unsigned char dlc;
	unsigned char data[8];
} rCanMsgBuff;


extern volatile unsigned char Can_Page;

/* Exported constants --------------------------------------------------------*/
#define CanSavePg()     (Can_Page = CAN_FPSR)
#define CanRestorePg()  (CAN_FPSR = Can_Page)

/*-- Bit defines -----------------------------------------------------------*/
#define CAN_TXMB0_PG       ((unsigned char) 0) /* CAN TX mailbox 0 reg page */
#define CAN_TXMB1_PG       ((unsigned char) 1) /* CAN TX mailbox 1 reg page */
#define CAN_FILTER01_PG    ((unsigned char) 2) /* CAN Filters 0 & 1 reg page*/
#define CAN_FILTER23_PG    ((unsigned char) 3) /* CAN Filters 2 & 3 reg page*/
#define CAN_FILTER45_PG    ((unsigned char) 4) /* CAN Filters 4 & 5 reg page*/
#define CAN_TXMB2_PG       ((unsigned char) 5) /* CAN TX mailbox 2 reg page */
#define CAN_CTRL_PG        ((unsigned char) 6) /* CAN control/status reg page*/
#define CAN_FIFO_PG        ((unsigned char) 7) /* CAN FIFO registers page */

#define MCSR_ABRQ          ((unsigned char)0x40)
#define MCSR_TXRQ          ((unsigned char)0x01)
#define MCSR_RQCP          ((unsigned char)0x04)

#define CMCR_TTCN          ((unsigned char)0x80)
#define CMCR_ABOM          ((unsigned char)0x40)
#define CMCR_AWUM          ((unsigned char)0x20)
#define CMCR_NART          ((unsigned char)0x10)
#define CMCR_RFLM          ((unsigned char)0x08)
#define CMCR_TXFP          ((unsigned char)0x04)
#define CMCR_SLEEP         ((unsigned char)0x02)
#define CMCR_INRQ          ((unsigned char)0x01)

#define CMSR_SLAK          ((unsigned char)0x02)
#define CMSR_INAK          ((unsigned char)0x01)
#define CMSR_ERRI          ((unsigned char)0x04)
#define CMSR_WKUI          ((unsigned char)0x08)
#define CMSR_REC           ((unsigned char)0x20)

#define CTSR_RQCP0         ((unsigned char)0x01)
#define CTSR_RQCP1         ((unsigned char)0x02)
#define CTSR_RQCP2         ((unsigned char)0x04)
#define CTSR_TXOK0         ((unsigned char)0x10)
#define CTSR_TXOK1         ((unsigned char)0x20)

#define CTPR_TME0          ((unsigned char)0x04)
#define CTPR_TME1          ((unsigned char)0x08)
#define CTPR_TME2          ((unsigned char)0x10)

#define CRFR_RFOM          ((unsigned char)0x20)
#define CRFR_FOVR          ((unsigned char)0x10)
#define CRFR_FULL          ((unsigned char)0x08)
#define CRFR_FMP01         ((unsigned char)0x03)

#define CESR_BOFF          ((unsigned char)0x04)
#define CESR_EPVF          ((unsigned char)0x02)
#define CESR_EWGF          ((unsigned char)0x01)

#define CDGR_RX            ((unsigned char)0x08)
#define CDGR_3TX           ((unsigned char)0x10)
#define CDGR_LBKM          ((unsigned char)0x01)
#define CDGR_SILM          ((unsigned char)0x02)


#define CIER_WKUIE         0x80
#define CIER_FOVIE         0x08
#define CIER_FFIE          0x04
#define CIER_FMPIE         0x02
#define CIER_TMEIE         0x01

#define CEIER_ERRIE        0x80
#define CEIER_LECIE        0x10
#define CEIER_BOFIE        0x04
#define CEIER_EPVIE        0x02
#define CEIER_EWGIE        0x01

////////////////////////////////////////////////////////////////////////////////
///////////////////added by sherry li///////////////////////////////////////////
/*-- CAN Filter Parameters ------------------------------------------------*/
#define CAN_FCR1_FILTER0_ACTIVE		0x01
#define CAN_FCR1_FILTER1_ACTIVE		0x10
#define CAN_FCR2_FILTER2_ACTIVE		0x01
#define CAN_FCR2_FILTER3_ACTIVE		0x10
#define CAN_FCR3_FILTER4_ACTIVE		0x01
#define CAN_FCR3_FILTER5_ACTIVE		0x10

#define CAN_FCR1_FILTER0_SCALE_1_32BIT	0x06
#define CAN_FCR1_FILTER0_SCALE_2_16BIT	0x04
#define CAN_FCR1_FILTER0_SCALE_1_16BIT_2_8BIT	0x02
#define CAN_FCR1_FILTER0_SCALE_4_8BIT		0x00

#define CAN_FCR1_FILTER1_SCALE_1_32BIT	0x60
#define CAN_FCR1_FILTER1_SCALE_2_16BIT	0x40
#define CAN_FCR1_FILTER1_SCALE_1_16BIT_2_8BIT	0x20
#define CAN_FCR1_FILTER1_SCALE_4_8BIT		0x00

#define CAN_FCR2_FILTER2_SCALE_1_32BIT	0x06
#define CAN_FCR2_FILTER2_SCALE_2_16BIT	0x04
#define CAN_FCR2_FILTER2_SCALE_1_16BIT_2_8BIT	0x02
#define CAN_FCR2_FILTER2_SCALE_4_8BIT		0x00

#define CAN_FCR2_FILTER3_SCALE_1_32BIT	0x60
#define CAN_FCR2_FILTER3_SCALE_2_16BIT	0x40
#define CAN_FCR2_FILTER3_SCALE_1_16BIT_2_8BIT	0x20
#define CAN_FCR2_FILTER3_SCALE_4_8BIT		0x00

#define CAN_FCR3_FILTER4_SCALE_1_32BIT	0x06
#define CAN_FCR3_FILTER4_SCALE_2_16BIT	0x04
#define CAN_FCR3_FILTER4_SCALE_1_16BIT_2_8BIT	0x02
#define CAN_FCR3_FILTER4_SCALE_4_8BIT		0x00

#define CAN_FCR3_FILTER5_SCALE_1_32BIT	0x60
#define CAN_FCR3_FILTER5_SCALE_2_16BIT	0x40
#define CAN_FCR3_FILTER5_SCALE_1_16BIT_2_8BIT	0x20
#define CAN_FCR3_FILTER5_SCALE_4_8BIT		0x00

#define CAN_FMR1_FILTER0_LR_LIST	0x01
#define CAN_FMR1_FILTER0_HR_LIST	0x02
#define CAN_FMR1_FILTER1_LR_LIST	0x04
#define CAN_FMR1_FILTER1_HR_LIST	0x08
#define CAN_FMR1_FILTER2_LR_LIST	0x10
#define CAN_FMR1_FILTER2_HR_LIST	0x20
#define CAN_FMR1_FILTER3_LR_LIST	0x40
#define CAN_FMR1_FILTER3_HR_LIST	0x80

#define CAN_FMR2_FILTER4_LR_LIST	0x01
#define CAN_FMR2_FILTER4_HR_LIST	0x02
#define CAN_FMR2_FILTER5_LR_LIST	0x04
#define CAN_FMR2_FILTER5_HR_LIST	0x08
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

/*-- CAN bit timing parameters ------------------------------------------------*/
/* Note: 8 <= total number of TIME QUANTUM (Tq) <= 25 */
/*******************************************************************************
 Constant Name: SJW
 Description  : Resynchronization Jump Width
 Value range  : from 1 to 4
 Comments     : The decrease by 1 for register write is made automatically.
*******************************************************************************/
#define CAN_SJW ((unsigned char) 3)

/*******************************************************************************
Constant name: BRP
Description  : Baud Rate Prescaler - define length of Tq
Value range  : from 1 to 64
Comments     : The decrease by 1 for register write is made automatically.
*******************************************************************************/
/* Baud rate = 125 Kbps @ (16 Tq & Fcpu = 8 MHz) */
//#define CAN_BRP ((unsigned char) 8)

/* Baud rate = 100 Kbps @ (16 Tq & Fcpu = 8 MHz) */

/* Baud rate = 33.3333 Kbps @ (20 Tq & Fcpu = 8 MHz) */
#define CAN_BRP ((unsigned char) 4)//6 4

/*******************************************************************************
Constant name: TSEG1
Description  : Time Segment 1 (+ prop. seg.)
Value range  : from 1 to 16              
Comments     : The decrease by 1 for register write is made automatically.
*******************************************************************************/
#define CAN_TSEG1 ((unsigned char) 4)

/*******************************************************************************
Constant name: TSEG2
Description  : Time Segment 2
Value range  : from 1 to 8        
Comments     : The decrease by 1 for register write is made automatically.
*******************************************************************************/
#define CAN_TSEG2 ((unsigned char) 3)

//SJW=2,BS2=2,BS1=14
/* CAN BIT TIMING REGISTERS */
#define CAN_CBTR0_REGISTER (unsigned char)  ( ((CAN_SJW-1 & 0x03)<<6) | ((CAN_BRP-1) & 0x3f) )
#define CAN_CBTR1_REGISTER (unsigned char)  ( ((CAN_TSEG2-1 & 0x07)<<4) | ((CAN_TSEG1-1) & 0x0f) |0x80)


/* Exported macro ------------------------------------------------------------*/
/* Exported functions ------------------------------------------------------- */
void Stm8_can_init (unsigned char);
void Stm8_can_wakeup(void);
void Stm8_can_restore (void);
#endif